from PyQt5.QtWidgets import QMessageBox

from src.admin.LR_UI import Ui_Form
from PyQt5 import QtWidgets


class LR_Win(QtWidgets.QMainWindow,Ui_Form):
    def __init__(self,mydb, parent=None):
        super(LR_Win, self).__init__(parent=parent)
        #ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('')
        #self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())
        #self.setWindowIcon(icon)
        self.setupUi(self)
        self.mydb=mydb

    def myOut(self):
        self.hide()

    def getmydb(self,mydb):
        self.mydb=mydb

    def myCalcu(self):
        myselect = "select 标准价格,损旧等级,是否租用 from 玩具个体表 where 玩具编号 =" + self.lineEdit_2.text()
        mycursor = self.mydb.cursor()
        try:
            mycursor.execute(myselect)
            result = mycursor.fetchall()
        except Exception as a:
            print(a)
            self.label_8.setText(str(0))
            QMessageBox.critical(self, '错误', '玩具号错误')
        else:
            if len(result)==0:
                self.label_8.setText(str(0))
                QMessageBox.critical(self, '错误', '玩具号不存在')
            elif result[0][2]=="不可租用":
                self.label_8.setText(str(0))
                QMessageBox.critical(self, '错误', '玩具已租用')
            else:
                data = int(self.spinBox.text())
                price = float(result[0][0])
                old = float(result[0][1])
                price2 = price * (1 - old / 10) * (1 + data / 10)
                self.label_8.setText(str(price2))

    def myInsert(self):
        if self.label_8.text()!="0":
            toyid = self.lineEdit_2.text()
            userid = self.lineEdit.text()
            mycusor = self.mydb.cursor()
            mycusor.execute("select 玩具种类号 from 玩具个体表 where 玩具编号 = "+toyid)
            result = mycusor.fetchall()
            toykindid =int(result[0][0])
            count = self.label_8.text()
            borrowday = self.dateEdit.text()
            returnday = self.dateEdit.date().addDays(int(self.spinBox.text()))
            now = returnday.toPyDate()
            overdue = '否'
            try:
                data = (int(toyid), int(userid), toykindid, float(count), borrowday, str(now), overdue)
                mycusor = self.mydb.cursor()
                mycusor.execute("insert into 出租信息表 value('%d','%d','%d','%f','%s','%s','%s')" % (data))
                self.mydb.commit()
            except Exception as a:
                QMessageBox.critical(self, '错误', '出租失败')

            else:
                QMessageBox.information(self, '成功', '出租成功')
        else:
            QMessageBox.critical(self, '错误', '请先计算')

    def myChange(self):
        self.label_8.setText('0')

